package com.itttq.kb.controller;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.kb.model.po.UserPo;
import com.itttq.kb.model.to.CatchDataTo;
import com.itttq.kb.model.to.PageParamTo;
import com.itttq.kb.service.TopicService;
import com.itttq.kb.utils.Constants;

public abstract class GenericController implements Serializable{
	private static final long serialVersionUID = -8751733063972170283L;
	@Autowired
	protected TopicService topicService;
	// 记录缓存数据
	public static CatchDataTo catchDataTo = new CatchDataTo();
	
	/**
	 * 获取缓存中用户信息、最热帖子、最新帖子。
	 * 
	 * @param mv
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	public ModelAndView getUserAndTopic(Integer catId, ModelAndView mv, HttpServletRequest request) {
		// 获取当前登录的用户信息
		mv.addObject("user", getUserBySession(request));
		// 获取最热帖子
		mv.addObject("mostLookTopics", topicService.getMostRead10Topic(catId, 10));
		// 获取热议帖子
		mv.addObject("mostReplyTopics", topicService.getMostReply10Topic(catId, 10));
		return mv;
	}
	
	/**
	 * 从request中获取httpSession
	 * 
	 * @param request HttpServletRequest请求
	 * @return HttpSession对象
	 */
	public HttpSession getSession(HttpServletRequest request) {
		return request.getSession();
	}

	/**
	 * 设置值到session中
	 * 
	 * @param request request请求
	 * @param name session中名称
	 * @param value session中值
	 */
	public void setSession(HttpServletRequest request, String name, Object value) {
		this.getSession(request).setAttribute(name, value);
	}

	/**
	 * 设置值到session中
	 * 
	 * @param request request请求
	 * @param name session中名称
	 */
	public Object getSession(HttpServletRequest request, String name) {
		return this.getSession(request).getAttribute(name);
	}
	
	/**
	 * @Description: 从http请求中获取分页参数，返回分页的Map对象。
	 * @author: 迷蝶
	 * @param: @param request
	 * @param: @return
	 * @return: Map<String,Object>
	 * @throws
	 */
	public PageParamTo getPageParam(HttpServletRequest request) {
		String pageNoStr = request.getParameter("pn");
		String pageSizeStr = request.getParameter("ps");
		int start = 0;
		int pageSize;
		if (!StringUtils.isEmpty(pageSizeStr)) {
			pageSize = Integer.parseInt(pageSizeStr);
			if(pageSize > 100) pageSize = 100;
		} else {
			pageSize = 10;
		}
		int pageNo;
		if (!StringUtils.isEmpty(pageNoStr)) {
			pageNo = Integer.parseInt(pageNoStr);
			if (pageNo > 0) {
				start = (pageNo - 1) * pageSize;
			}
		} else {
			pageNo = 1;
		}
		PageParamTo ppt = new PageParamTo();
		ppt.setNo(pageNo);
		ppt.setSize(pageSize);
		ppt.setStart(start);
		return ppt;
	}
	
	/**
	 * 
	 * 默认初始化分页类。默认start=0；no=1。需要自己传入每页数据量
	 * @param ps
	 * @return
	 * @Author 迷蝶
	 * @return PageParamTo
	 */
	public PageParamTo getPageParam(int ps) {
		PageParamTo ppt = new PageParamTo();
		ppt.setNo(1);
		ppt.setSize(ps);
		ppt.setStart(0);
		return ppt;
	}
	
	/**
	 * 获取分页modelAndView
	 * 
	 * @param attrName
	 * @param mv
	 * @param t
	 * @param count
	 * @param ppt
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	public ModelAndView getPageMv(String attrName, 
			ModelAndView mv, Object t, int count, PageParamTo ppt) {
		mv.addObject(attrName, t);
		mv.addObject("count", count);
		mv.addObject("pn", ppt.getNo());
		mv.addObject("ps", ppt.getSize());
		
		return mv;
	}
	

	/**
	 * 从session中获取user对象
	 * 
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return UserPo
	 */
	public UserPo getUserBySession(HttpServletRequest request) {
		Object obj = this.getSession(request, Constants.SESSION_USER);
		UserPo user = null;
		if(null != obj) {
			user = (UserPo) obj;
		}
		return user;
	}
	
}

